package com.zw.yzk.kabu.utils;

import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by zhanwei on 2017/5/17.
 */

public class StringUtils {
    /**
     * 找出两#之间的字符串
     */
    public static final String DIVIDE_STRING_PATTERN1 = "#[^#]+#";
    public static final String DIVIDE_STRING_PATTERN2 = "@(\\S+)\\s";

    public static List<String> divideString(String source, String pattern) {
        List<String> result = new ArrayList<>();
        if (TextUtils.isEmpty(source)) {
            return result;
        }
        if (TextUtils.isEmpty(pattern)) {
            result.add(source);
            return result;
        }
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(source);
        while (m.find()) {
            result.add(m.group());
        }
        return result;
    }

    /**
     * 给特定格式的文本着色
     *
     * @param source  源文本
     * @param pattern 格式
     * @param color   颜色
     * @return SpannableString
     */
    public static SpannableString colorTextWithPattern(String source, String pattern, int color) {
        if(TextUtils.isEmpty(source)) {
            return new SpannableString("");
        }
        SpannableString spanText = new SpannableString(source);
        List<String> patternResult = StringUtils.divideString(source, pattern);
        String remainString = source;
        int realStart = 0;
        for (String str : patternResult) {
            realStart += remainString.indexOf(str);
            spanText.setSpan(new ForegroundColorSpan(color), realStart, realStart + str.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            remainString = remainString.substring(remainString.indexOf(str) + str.length());
            realStart += str.length();
        }

        return spanText;
    }

    /**
     * 给特定格式的文本着色
     *
     * @param source  源文本
     * @param pattern 格式
     * @param color   颜色
     * @return SpannableString
     */
    public static SpannableString colorTextWithPattern(SpannableString source, String pattern, int color) {
        if(source == null) {
            return new SpannableString("");
        }
        SpannableString spanText = source;
        List<String> patternResult = StringUtils.divideString(source.toString(), pattern);
        String remainString = source.toString();
        int realStart = 0;
        for (String str : patternResult) {
            realStart += remainString.indexOf(str);
            spanText.setSpan(new ForegroundColorSpan(color), realStart, realStart + str.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            remainString = remainString.substring(remainString.indexOf(str) + str.length());
            realStart += str.length();
        }

        return spanText;
    }


    /**
     * 给特定格式的文本着色
     *
     * @param source 源文本
     * @param start  开始
     * @param end    结束
     * @param color  颜色
     * @return SpannableString
     */
    public static SpannableString colorText(String source, int start, int end, int color) {
        if(TextUtils.isEmpty(source)) {
            return new SpannableString("");
        }
        SpannableString spanText = new SpannableString(source);
        spanText.setSpan(new ForegroundColorSpan(color), start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        return spanText;
    }

    /**
     * 添加点击事件
     *
     * @param source 源文本
     * @param start  开始
     * @param end    结束
     * @return SpannableString
     */
    public static SpannableString clickableText(String source, int start, int end, ClickableSpan clickableSpan) {
        if(TextUtils.isEmpty(source)) {
            return new SpannableString("");
        }
        SpannableString spanText = new SpannableString(source);
        spanText.setSpan(clickableSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanText;
    }

    /**
     * 给特定格式文本着色并添加点击事件
     *
     * @param source 源文本
     * @param start  开始
     * @param end    结束
     * @return SpannableString
     */
    public static SpannableString colorAndClickableText(String source, int start, int end, int color, ClickableSpan clickableSpan) {
        if(TextUtils.isEmpty(source)) {
            return new SpannableString("");
        }
        SpannableString spanText = new SpannableString(source);
        spanText.setSpan(new ForegroundColorSpan(color), start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        spanText.setSpan(clickableSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanText;
    }

}
